package tutorial.java.dragdrop;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;

import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTextField;

import java.awt.event.MouseMotionAdapter;

public class MouseEventDemo extends JFrame {

	private JPanel contentPane;
	private JTextArea textArea;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MouseEventDemo frame = new MouseEventDemo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MouseEventDemo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textArea = new JTextArea();
		textArea.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
					textArea.append("Left is clicked (" + e.getPoint().x +
							"," + e.getPoint().y + " ) \n");
				}
				if (e.getModifiers() == InputEvent.BUTTON2_MASK) {
					textArea.append("Middle is clicked \n");
				}
				if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
					textArea.append("Right is clicked \n");
				}
			}
		});
		textArea.setBounds(126, 130, 197, 119);
		contentPane.add(textArea);
		
		textField = new JTextField("New text");
		textField.setToolTipText("This is tool tip");
		textField.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				textField.setForeground(Color.RED);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				textField.setForeground(Color.BLUE);
			}
		});
		textField.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				textField.selectAll();
			}
		});
		textField.setBounds(126, 79, 132, 27);
		contentPane.add(textField);
		textField.setColumns(10);
	}
}
